﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Reborn_WorldServer
{
    public class LocalWorldsManager
    {
        private List<OwnedWorld> ownedWorlds = new List<OwnedWorld>();

        public void updateWorldsFromXML(string e)
        {
            lock (ownedWorlds)
            {
                List<OwnedWorld> updateWorlds = new List<OwnedWorld>();
                StringReader sr = new StringReader(e);
                XmlTextReader xmlReader = new XmlTextReader(sr);
                while (xmlReader.Read())
                {
                    if (xmlReader.Name == "ownedworld")
                    {
                        OwnedWorld world = new OwnedWorld();
                        world.MapID = int.Parse(xmlReader.GetAttribute("mapid").ToString());
                        world.Instance = int.Parse(xmlReader.GetAttribute("instance").ToString());
                        updateWorlds.Add(world);
                    }
                }
                
                //Delete worlds we do not own anymore
                for (int x = 0; x < ownedWorlds.Count; x++)
                {
                    OwnedWorld world = ownedWorlds[x];

                    bool foundWorld = false;
                    foreach (OwnedWorld updateWorld in updateWorlds)
                    {
                        if (updateWorld.MapID == world.MapID && updateWorld.Instance == world.Instance)
                        {
                            foundWorld = true;
                            break;
                        }
                    }
                    if (foundWorld == false)
                    {
                        ownedWorlds.RemoveAt(x);
                        x--;
                    }
                }

                foreach (OwnedWorld updateWorld in updateWorlds)
                {
                    bool foundWorld = false;
                    foreach (OwnedWorld world in ownedWorlds)
                    {
                        if (world.MapID == updateWorld.MapID && world.Instance == updateWorld.Instance)
                        {
                            foundWorld = true;
                            break;
                        }
                    }
                    if (foundWorld == false)
                        ownedWorlds.Add(updateWorld);
                }

                foreach (OwnedWorld world in ownedWorlds)
                    Managers.GameHandler.GetLockItem(world.MapID, world.Instance);

            }
            sendWorldsToPrimary();
        }

        public void sendWorldsToPrimary()
        {
            try
            {
                lock (ownedWorlds)
                {
                    string result = "";

                    StringWriter stringWriter = new StringWriter();
                    XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

                    xmldoc.WriteStartElement("ownedworlds");

                    foreach (OwnedWorld world in ownedWorlds)
                        xmldoc.WriteRaw(world.toXML());

                    xmldoc.WriteEndElement();

                    xmldoc.Flush();
                    xmldoc.Close();

                    stringWriter.Flush();

                    result = stringWriter.ToString();

                    byte[] byData = System.Text.Encoding.ASCII.GetBytes(result + "\0");
                    Managers.socketPrimary.socket.Send(byData);
                }
            }
            catch { }
        }

        public bool ownsWorld(int MapID, int Instance)
        {
            lock (ownedWorlds)
            {
                foreach (OwnedWorld world in ownedWorlds)
                {
                    if (world.MapID == MapID && world.Instance == Instance)
                        return true;
                }
            }
            return false;
        }
    }
    class OwnedWorld
    {
        public int MapID;
        public int Instance;

        public string toXML()
        {
            return "<ownedworld mapid=\"" + MapID.ToString() + "\" instance=\"" + Instance.ToString() + "\" />";
        }
    }
}
